home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Code Resources / CDEF - DeBugger 2.0.2 / Error.c < prev    next >
Encoding:
Text File  |  1995-10-19  |  3.2 KB  |  102 lines  |  [TEXT/KAHL]

  1. //     Error.c
  2. //
  3. //    A simply little error window to display a message sent by the
  4. //    programmer. I use it to tell me what I was doing before I bombed!
  5. //
  6. //    This file needs "errorIcon.r" to work
  7. //    use this to report an error to the user.  Better used for debugging, but it 
  8. //    could be modified any way you see fit. Uses the icon provided in "errorIcon.r"
  9.  
  10. // Prototypes
  11.     void    ReportError( Str255    errorMsg );
  12.     Boolean    CompareStr( Str255 srcA, Str255 srcB );
  13.  
  14. // globals
  15.     extern Main        myMain;
  16.  
  17. /******************************************************************************
  18.  
  19.   A simple procedure for reporting errors.
  20.   
  21. *******************************************************************************/
  22. void    ReportError( Str255    errorMsg )
  23. {
  24.     short            maxStrSize, iconID, winHorz, winVert, strWidth;
  25.     Rect            theRect;
  26.     GrafPtr            thePort;
  27.     Point            localPt;
  28.     
  29.     
  30.     // store the current port and set up some intial variables
  31.         GetPort( &thePort );
  32.     
  33.     // make sure the error string isn't to large.
  34.         if( errorMsg[0] > 128 ) {
  35.             errorMsg[0] = 128;
  36.         }
  37.     
  38.     // grab our icon dude.
  39.         myMain.icon = GetCIcon( 32000 );
  40.         HLock( (Handle)myMain.icon );
  41.     
  42.     // position the window in global coordinates to center on the main screen.
  43.         SetRect( &theRect, 0, 0, 400, 200 );
  44.         myMain.error = NewCWindow(     nil, &theRect, "\p", FALSE, dBoxProc, 
  45.                                     (WindowPtr)-1, false, ERROR_WINDOW );
  46.         SetPort( myMain.error );
  47.     
  48.     // set the text stuff up
  49.         TextFont( 9 );
  50.         TextSize( 9 );
  51.         TextFace( 0 );
  52.         strWidth = StringWidth( errorMsg );
  53.     
  54.     // add the size of our icon and the space between it and our text and space along the
  55.     // window frame. we will use this to size our window.
  56.         strWidth += 32 + 14 + 28;
  57.     
  58.     // make sure that it's not to small. just in case.
  59.         if( strWidth < 100 ) {
  60.             strWidth = 100;
  61.         }
  62.     
  63.     // no let's resize our window to fit the size.
  64.         winHorz = ( qd.screenBits.bounds.right - qd.screenBits.bounds.left ) / 2;
  65.         winVert    = ( qd.screenBits.bounds.bottom - qd.screenBits.bounds.top ) / 3;
  66.         SizeWindow( myMain.error, strWidth, 60, TRUE );
  67.         MoveWindow( myMain.error, winHorz - (strWidth / 2), winVert, TRUE );
  68.         ShowWindow( myMain.error );
  69.     
  70.     // draw our nifty icon dude.
  71.         SetRect( &theRect, (*myMain.error).portRect.left + 14, (*myMain.error).portRect.top + 14,
  72.                            (*myMain.error).portRect.left + 46, (*myMain.error).portRect.top + 46 );
  73.         PlotCIcon( &theRect, myMain.icon );
  74.     
  75.     // draw the error message.
  76.         MoveTo( 60, 25 );
  77.         TextFace( 1 );
  78.         DrawString( "\pError Report:" );
  79.         TextFace( 0 );
  80.         MoveTo( 60, 40 );
  81.         DrawString( errorMsg );
  82.     
  83.     // store the message for later retrieval
  84.         BlockMove( &errorMsg[0], &myMain.errMsg[0], errorMsg[0] );
  85.     
  86.     // do a system beep just for the excitement of things.
  87.         SysBeep( 1 );
  88.     
  89.     // just create a little loop untill the user presses the mouse button
  90.         do {
  91.         } while ( !Button());
  92.     
  93.     // Handle anything we might of done.
  94.         HUnlock( (Handle)myMain.icon );
  95.         DisposCIcon( myMain.icon );
  96.         DisposeWindow( myMain.error );
  97.     
  98.     // Exit the application. This could be avoided if perhaps you test for a fatal error
  99.     // then just past TRUE or FALSE to exit. But I'm to lazy, it either works or it doesn't
  100.     // Also you may wish to clean some things up before you do this.
  101.         ExitToShell();
  102. }